package gulimall.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import gulimall.entity.Brand;
import gulimall.entity.CategoryBrandRelation;
import gulimall.mapper.BrandMapper;
import gulimall.mapper.CategoryBrandRelationMapper;
import gulimall.service.CategoryBrandRelationService;
import gulimall.service.IBrandService;
import gulimall.service.ICategoryService;
import gulimall.utils.PageUtils;
import gulimall.utils.Query;
import gulimall.vo.BrandRespVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelation> implements CategoryBrandRelationService {

    @Resource
    private IBrandService brandService;

    @Resource
    private BrandMapper brandMapper;

    @Resource
    private ICategoryService categoryService;

    @Resource
    private CategoryBrandRelationMapper categoryBrandRelationMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelation> page = this.page(
                new Query<CategoryBrandRelation>().getPage(params),
                new QueryWrapper<CategoryBrandRelation>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveDetail(CategoryBrandRelation categoryBrandRelation) {
        //获取设置品牌名和分类名
        String brandName = brandService.getById(categoryBrandRelation.getBrandId()).getName();
        String categoryName = categoryService.getById(categoryBrandRelation.getCatelogId()).getName();

        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(categoryName);
    }

    /**
     * 更新pms_category_brand_relation中Brand品牌的相关信息
     *
     * @param brandId
     * @param name
     */
    @Override
    @Transactional
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelation categoryBrandRelation = new CategoryBrandRelation();
        categoryBrandRelation.setBrandId(brandId);
        categoryBrandRelation.setBrandName(name);
        categoryBrandRelationMapper.update(categoryBrandRelation, new UpdateWrapper<CategoryBrandRelation>()
                .lambda().eq(CategoryBrandRelation::getBrandId, brandId));
    }

    /**
     * 更新pms_category_brand_relation中Category分类的相关信息
     *
     * @param catId
     * @param name
     * @return
     */
    @Override
    @Transactional
    public int updateCategory(Long catId, String name) {
        CategoryBrandRelation categoryBrandRelation = new CategoryBrandRelation();
        categoryBrandRelation.setCatelogId(catId);
        categoryBrandRelation.setCatelogName(name);
        return categoryBrandRelationMapper.update(categoryBrandRelation,
                new UpdateWrapper<CategoryBrandRelation>()
                        .lambda().eq(CategoryBrandRelation::getCatelogId, catId));
    }

    /**
     * 获取分类关联的品牌
     *
     * @param catId 分类id
     * @return
     */
    @Override
    public List<BrandRespVo> listBrandsByCatId(Long catId) {
        List<CategoryBrandRelation> relations = categoryBrandRelationMapper.selectList(new QueryWrapper<CategoryBrandRelation>()
                .lambda().eq(CategoryBrandRelation::getCatelogId, catId)
        );
        List<Long> brandIds = relations.stream().map(CategoryBrandRelation::getBrandId).collect(Collectors.toList());
        if (brandIds.size() > 0) {
            List<Brand> brands = brandService.listByIds(brandIds);
            List<BrandRespVo> brandRespVos = brands.stream().map(brand -> {
                BrandRespVo brandRespVo = new BrandRespVo();
                brandRespVo.setBrandId(brand.getBrandId());
                brandRespVo.setBrandName(brand.getName());
                return brandRespVo;
            }).collect(Collectors.toList());
            return brandRespVos;
        } else {
            return null;
        }
    }

}